/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManagerFilesJIF.java
 *
 * Created on Oct 28, 2011, 11:58:14 AM
 */
package breakers_cna_client.requests;

import breakers_cna_client.MainForm;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import own_packets.Protocol;
import threads.DownloadConnection;
import threads.SearchConnection;

/**
 *
 * @author THUY TIEN
 */
public class ManagerFilesJIF extends javax.swing.JInternalFrame {

    private HistoryJIF _historyFrame;
    private JFrame _parent;
    private String _host;
    private Integer _port;
    private MainForm _mainForm;
    private Thread _searchThread, _downloadThread;
    private SearchConnection _searchProcces;
    private DownloadConnection _downloadProccess;
    private String _downloadHost;
    private Integer _downloadPort;

    public String getDownloadHost() {
        return _downloadHost;
    }

    public void setDownloadHost(String _downloadHost) {
        this._downloadHost = _downloadHost;
    }

    public Integer getDownloadPort() {
        return _downloadPort;
    }

    public void setDownloadPort(Integer _downloadPort) {
        this._downloadPort = _downloadPort;
    }

    public String getfName() {
        return _fName;
    }

    public void setfName(String _fName) {
        this._fName = _fName;
    }

    public String getUrlName() {
        return _urlName;
    }

    public void setUrlName(String _urlName) {
        this._urlName = _urlName;
    }
    private String _fName, _urlName;

    /** Creates new form ManagerFilesJIF */
    public ManagerFilesJIF() {
        initComponents();
        this.setSize(700, 410);

        _historyFrame = new HistoryJIF();

    }

    public ManagerFilesJIF(JFrame parent) {
        initComponents();
//        this.setSize(700, 410);
        _parent = parent;
//        _historyFrame = new HistoryJIF();
//        _parent.add(_historyFrame);
    }

    public String getHost() {
        return _host;
    }

    public void setHost(String _host) {
        this._host = _host;
    }

    public Integer getPort() {
        return _port;
    }

    public void setPort(Integer _port) {
        this._port = _port;
    }

//    public ManagerFilesJIF(MainForm parent) {
//        initComponents();
//        this.setSize(700, 410);
//        _mainForm = parent;
//        _historyFrame = new HistoryJIF();
//        _parent.add(_historyFrame);
//    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        tbManagerFiles = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        txtSearch = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        btnViewHistory = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Manage Request Files"); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tbManagerFiles.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Tên tập tin", "Địa chỉ IP", "Trạng thái", "Tải về"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbManagerFiles.setName("tbManagerFiles"); // NOI18N
        jScrollPane2.setViewportView(tbManagerFiles);
        tbManagerFiles.getColumnModel().getColumn(0).setPreferredWidth(40);
        tbManagerFiles.getColumnModel().getColumn(0).setMaxWidth(40);
        tbManagerFiles.getColumnModel().getColumn(3).setPreferredWidth(80);
        tbManagerFiles.getColumnModel().getColumn(3).setMaxWidth(80);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        txtSearch.setName("txtSearch"); // NOI18N

        btnSearch.setText("Tìm kiếm"); // NOI18N
        btnSearch.setName("btnSearch"); // NOI18N
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnViewHistory.setText("Xem lịch sử"); // NOI18N
        btnViewHistory.setName("btnViewHistory"); // NOI18N
        btnViewHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewHistoryActionPerformed(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(ManagerFilesJIF.class);
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(btnViewHistory)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 171, Short.MAX_VALUE)
                .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnSearch)
                .addGap(25, 25, 25))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnViewHistory)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(198, 198, 198)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(63, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnViewHistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewHistoryActionPerformed
        // TODO add your handling code here:
        if (_historyFrame == null) {
            System.out.println("History Frame is null ");
            return;
        }
        _historyFrame.setVisible(true);
    }//GEN-LAST:event_btnViewHistoryActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        if (_host == null || _port == null) {
            System.out.println("Host, port not setup !");
            return;
        }

        if (_searchThread == null && _searchProcces == null) {
            String textSearch = this.txtSearch.getText();
            _searchProcces = new SearchConnection(_host, _port);
            _searchProcces.setSearch(textSearch);
            _searchThread = new Thread(_searchProcces);
            _searchThread.start();
        }


//        try {
//            Protocol obj = _searchProcces.SendSearchMessages(textSearch);
//            String messages = (obj == null) ? "Recevied Search Failed": obj.getMessages();
//            System.out.println(String.format("Server response : %s", messages));
//        } catch (IOException ex) {
//            System.out.println(String.format("Failed in send search request from client : %s", ex.getMessage()));
//        } catch (ClassNotFoundException ex) {
//            System.out.println(String.format("Failed in send search request from client : %s", ex.getMessage()));
//        }
    }//GEN-LAST:event_btnSearchActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (_searchProcces == null ){
             System.out.println("Search null !");
            return;
        }
        String downloadHost = _searchProcces.getDownloadHost();
        Integer downloadPort = _searchProcces.getDownloadPort();
        String fName = _searchProcces.getFileName();
        String urlName = _searchProcces.getUrlFile();
        if (downloadHost == null || downloadPort == null || fName == null || urlName == null) {
            System.out.println("Download Host, Download port, filename, urlname not found !");
            return;
        }
        if (_downloadThread == null && _downloadProccess == null) {
            _downloadProccess = new DownloadConnection(downloadHost, downloadPort, fName, urlName);
            _downloadThread = new Thread(_downloadProccess);
            _downloadThread.start();
        }

    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnViewHistory;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbManagerFiles;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
